# $LastChangedDate: 2011-09-25 12:03:59 -0400 (Sun, 25 Sep 2011) $
# LEGAL: COPYRIGHT (C) 2010 JIM E. BROOKS WWW.PALOMINO3D.ORG
# Find libraries.
# Find*.cmake should only do standard cmake actions: INCLUDE_DIRECTORES() etc.
# These higher-level *.cmake files can do special configuration
# such as #defines and modifying SYSTEM_LIB.
# Notes:
# - See module.txt.
#-------------------------------------------------------------------------------

# POSIX real-time library.
MACRO( COMPILE_WITH_POSIX_REALTIME )
    IF( UNIX AND NOT APPLE )
        SET( SYSTEM_LIBS ${SYSTEM_LIBS} rt )
    ENDIF()
ENDMACRO()

# Threading:
# COMPILE_THREADS=0 entirely disables compiling the threads modules.
# COMPILE_THREADS_SINGLE=1 compiles the threads modules for a single thread.
IF( COMPILE_THREADS )
    IF( COMPILE_THREADS_SINGLE OR WIN32 )
        # Single-thread.
        MESSAGE( STATUS "threads module: single-thread" )
        SET( COMPILE_THREADS_SINGLE "1" )
        ADD_DEFINITIONS( -DCOMPILE_THREADS=1 -DCOMPILE_THREADS_SINGLE=1 )
    ELSE()
        IF( UNIX OR APPLE )
            # POSIX pthreads.
            MESSAGE( STATUS "threads module: POSIX" )
            SET( COMPILE_THREADS_POSIX "1" )
            ADD_DEFINITIONS( -DCOMPILE_THREADS=1 -DCOMPILE_THREADS_POSIX=1 )
        ELSE()
            MESSAGE( FATAL_ERROR "Unsupported thread system." )
        ENDIF()
    ENDIF()
ENDIF()

# OpenGL.
MACRO( COMPILE_WITH_OPENGL )
    INCLUDE( FindOpenGL )
    IF( OPENGL_FOUND )
        MESSAGE( STATUS "Found OpenGL." )
       #ADD_DEFINITIONS( -DCOMPILE_OPENGL=1 )
       #ADD_DEFINITIONS( -DGFXSYS_OPENGL=1 )
        SET( SYSTEM_LIBS ${SYSTEM_LIBS} ${OPENGL_LIBRARIES} )
        ADD_SYSTEM_INCLUDE_DIR( ${OPENGL_INCLUDE_DIR} )
        IF( APPLE )
            # KLUDGE: cmake ignores OPENGL_INCLUDE_DIR because XCode 2.5 has a broken OpenGL symlink.
            ADD_SYSTEM_INCLUDE_DIR( "${OPENGL_INCLUDE_DIR}/Headers" )
        ENDIF()
    ENDIF()
ENDMACRO()

# OpenGL GLUT.
MACRO( COMPILE_WITH_OPENGL_GLUT )
    INCLUDE( FindGLUT )
    IF( GLUT_FOUND )
        MESSAGE( STATUS "Found OpenGL GLUT." )
        ADD_DEFINITIONS( -DCOMPILE_GLUT=1 )
        SET( SYSTEM_LIBS ${SYSTEM_LIBS} ${GLUT_LIBRARY} )
        ADD_SYSTEM_INCLUDE_DIR( ${GLUT_INCLUDE_DIR} )
    ENDIF()
ENDMACRO()

# OpenSceneGraph:
MACRO( COMPILE_WITH_OPENSCENEGRAPH )
    FIND_PACKAGE( OpenSceneGraph )
    FIND_PACKAGE( OpenThreads )
    ADD_SYSTEM_INCLUDE_DIR( ${OSG_INCLUDE_DIR} )
    LINK_DIRECTORIES( ${OSG_INCLUDE_DIR}/../lib )
    # KLUDGE: OpenSceneGraph's 3rdParty Windows libraries aren't compatible with debug builds.
    # Force compiler error (haven't found a better way to catch this).
    IF( OSG_FOUND AND WIN32 )
        SET( CMAKE_CXX_FLAGS_DEBUG "${PROJECT_NAME}_MUST_CONFIGURE_Release_OR_RelWithDebInfo_FOR_OSG_3RDPARTY" )
        MESSAGE( "(warning) Must select Release/RelWithDebInfo in Visual Studio (OSG incompatible with Debug)" )
    ENDIF()
ENDMACRO()

# zlib.
MACRO( COMPILE_WITH_ZLIB )
    FIND_PACKAGE( ZLIB )
    IF( ZLIB_FOUND )
        ADD_DEFINITIONS( -DCOMPILE_ZLIB=1 )
        SET( SYSTEM_LIBS ${SYSTEM_LIBS} ${ZLIB_LIBS} ${ZLIB_LIBRARY})
        if(WIN32)
          list(APPEND SYSTEM_LIBS legacy_stdio_definitions)
        endif()
    ELSE()
        IF( REQUIRE_ZLIB )
            MESSAGE( FATAL_ERROR "zlib package is required" )
        ENDIF()
    ENDIF()
ENDMACRO()

# SDL (Simple Direct-media Layer).
# Just the core SDL (excludes mixer, ttf, graphics, etc).
MACRO( COMPILE_WITH_SDL )
    INCLUDE( FindSDL )
    IF( SDL_FOUND )
        MESSAGE( STATUS "Found SDL: ${SDL_LIBRARY} ${SDL_INCLUDE_DIR}" )
        ADD_DEFINITIONS( -DCOMPILE_SDL=1 )
        SET( SYSTEM_LIBS ${SYSTEM_LIBS} ${SDL_LIBRARY} )
        ADD_SYSTEM_INCLUDE_DIR( ${SDL_INCLUDE_DIR} )
    ENDIF()
ENDMACRO()

# SDL mixer (audio/sound/music).
# @pre Must call COMPILE_WITH_SDL() and SDL_FOUND is true.
MACRO( COMPILE_WITH_SDL_MIXER )
    INCLUDE( FindSDL_mixer )
    IF( SDL_FOUND AND SDLMIXER_FOUND )
        MESSAGE( STATUS "Found SDL mixer." )
        ADD_DEFINITIONS( -DCOMPILE_SDL_MIXER=1 )
        SET( SYSTEM_LIBS ${SYSTEM_LIBS} ${SDLMIXER_LIBRARY} )
        ADD_SYSTEM_INCLUDE_DIR( ${SDLMIXER_INCLUDE_DIR} )  # differs on Apple
    ENDIF()
ENDMACRO()

# Linux joystick.
MACRO( COMPILE_WITH_LINUX_JOYSTICK )
    FIND_PACKAGE( LinuxJoystick )
    IF( LINUX_JOYSTICK_FOUND )
        ADD_DEFINITIONS( -DCOMPILE_LINUX_JOYSTICK=1 )
    ENDIF()
ENDMACRO()

# PLIB.
MACRO( COMPILE_WITH_PLIB )
    FIND_PACKAGE( PLIB )
    # zlib required because sound files are gzip-compressed.
    IF( PLIB_FOUND AND ZLIB_FOUND )
        ADD_DEFINITIONS( -DCOMPILE_PLIB=1 )
        SET( SYSTEM_LIBS ${SYSTEM_LIBS} plibsm plibsl plibul )
    ENDIF()
ENDMACRO()

# FLTK.
MACRO( COMPILE_WITH_FLTK )
    FIND_PACKAGE( FLTK )
    IF( FLTK_FOUND )
        ADD_DEFINITIONS( -DCOMPILE_GUI=1 )
        SET( SYSTEM_LIBS ${SYSTEM_LIBS} ${FLTK_LIBS} )
    ENDIF()
ENDMACRO()

# OSSIM.
MACRO( COMPILE_WITH_OSSIM )
    FIND_PACKAGE( OSSIM )
    IF( OSSIM_FOUND )
        ADD_DEFINITIONS( -DCOMPILE_OSSIM=1 )
        SET( SYSTEM_LIBS ${SYSTEM_LIBS} ossimPlanet ossim )
    ENDIF()
ENDMACRO()
